package com.xr.web.controller.miniprogram;

import com.xr.common.annotation.Log;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.enums.BusinessType;
import com.xr.miniprogram.domain.MiniProgramMenu;
import com.xr.miniprogram.domain.vo.MiniProgramMenuTree;
import com.xr.miniprogram.service.IMiniProgramMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: 服务模块-菜单接口
 *
 * @author: wls
 * @date: 2023/11/9 9:37
 * @version:1.0
 */

@Api("服务模块-菜单")
@RestController
@RequestMapping("/mini/menu")
public class MpMenuController extends BaseController {

    @Resource
    private IMiniProgramMenuService menuService;

    /**
     * 查询服务菜单树形结构
     * @return
     */
    @ApiOperation("获取服务菜单树形结构")
    @PreAuthorize("@ss.hasPermi('mini:menu:tree')")
    @GetMapping("/tree")
    public AjaxResult tree(){
        List<MiniProgramMenuTree> list= menuService.getMenuTreeList();
        return success(list);
    }

    /**
     * 查询服务菜单
     * @return
     */
    @ApiOperation("获取服务菜单")
    @PreAuthorize("@ss.hasPermi('mini:menu:list')")
    @GetMapping("/list")
    public AjaxResult list(){
        List<MiniProgramMenu> list= menuService.getMenuList();
        return success(list);
    }

    /**
     * 新增服务菜单
     * @param miniProgramMenu
     * @return
     */
    @ApiOperation("新增服务菜单")
    @PreAuthorize("@ss.hasPermi('mini:menu:add')")
    @Log(title = "服务菜单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@ApiParam(value = "服务菜单对象", required = true) @RequestBody MiniProgramMenu miniProgramMenu){
        //校验是否已存在
        if (!menuService.checkServiceNameUnique(miniProgramMenu))
        {
            return error("新增服务'" + miniProgramMenu.getMenuName() + "'失败，服务名称已存在");
        }
        return success(menuService.addMenu(miniProgramMenu));
    }

    /**
     * 修改服务菜单
     * @param miniProgramMenu
     * @return
     */
    @ApiOperation("修改服务菜单")
    @PreAuthorize("@ss.hasPermi('mini:menu:edit')")
    @Log(title = "服务菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@ApiParam(value = "服务菜单对象", required = true) @RequestBody MiniProgramMenu miniProgramMenu){
        //校验是否已存在
        if (!menuService.checkServiceNameUnique(miniProgramMenu))
        {
            return error("修改服务'" + miniProgramMenu.getMenuName() + "'失败，服务名称已存在");
        }
        return success(menuService.updateMenu(miniProgramMenu));
    }

    /**
     * 根据id获取服务菜单
     * @param menuId
     * @return
     */
    @ApiOperation("根据id获取服务菜单")
    @PreAuthorize("@ss.hasPermi('mini:menu:query')")
    @GetMapping("/queryById")
    public AjaxResult queryById(@ApiParam(value = "服务菜单id", required = true) @RequestParam("menuId")Long menuId){
        return success(menuService.getMenuById(menuId));
    }

    /**
     * 根据id删除服务菜单
     * @param menuId
     * @return
     */
    @ApiOperation("根据id删除服务菜单")
    @PreAuthorize("@ss.hasPermi('mini:menu:remove')")
    @Log(title = "服务菜单", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{menuId}")
    public AjaxResult delete(@PathVariable Long menuId){
        return success(menuService.deleteMenu(menuId));
    }
}
